/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: property.h
 *
 * Purpose: property implementation for base library
 *
 * Developer:
 *   wen.gu , 2019-10-08
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __PANDA_SYS_PROPERTY_H__
#define __PANDA_SYS_PROPERTY_H__

#include <string>
#include <map>

#include "panda/sys/sys_types.h"

namespace panda
{
namespace sys
{
/******************************************************************************
 **    MACROS
 ******************************************************************************/

 /******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/

typedef void (*OnPropertyChangeFunc)(void* opaque, const std::string& key, const std::string& val);

using PropertyList = std::map<std::string, std::string>;

 /******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
CORE_API bool PropertySet(const std::string& key, const std::string& val);
CORE_API bool PropertySet(const std::string& key, int8_t val);
CORE_API bool PropertySet(const std::string& key, int16_t val);
CORE_API bool PropertySet(const std::string& key, int32_t val);
CORE_API bool PropertySet(const std::string& key, int64_t val);
CORE_API bool PropertySet(const std::string& key, uint8_t val);
CORE_API bool PropertySet(const std::string& key, uint16_t val);
CORE_API bool PropertySet(const std::string& key, uint32_t val);
CORE_API bool PropertySet(const std::string& key, uint64_t val); 
CORE_API bool PropertySet(const std::string& key, float val);
CORE_API bool PropertySet(const std::string& key, double val);



CORE_API bool PropertyGet(const std::string& key, std::string& val);
CORE_API bool PropertyGet(const std::string& key, int8_t& val);
CORE_API bool PropertyGet(const std::string& key, int16_t& val);
CORE_API bool PropertyGet(const std::string& key, int32_t& val);
CORE_API bool PropertyGet(const std::string& key, int64_t& val);
CORE_API bool PropertyGet(const std::string& key, uint8_t& val);
CORE_API bool PropertyGet(const std::string& key, uint16_t& val);
CORE_API bool PropertyGet(const std::string& key, uint32_t& val);
CORE_API bool PropertyGet(const std::string& key, uint64_t& val);
CORE_API bool PropertyGet(const std::string& key, float& val);
CORE_API bool PropertyGet(const std::string& key, double& val);


CORE_API std::string PropertyGetOr(const std::string& key, const std::string& defVal);
CORE_API int8_t      PropertyGetOr(const std::string& key, int8_t   defVal);
CORE_API int16_t     PropertyGetOr(const std::string& key, int16_t  defVal);
CORE_API int32_t     PropertyGetOr(const std::string& key, int32_t  defVal);
CORE_API int64_t     PropertyGetOr(const std::string& key, int64_t  defVal);
CORE_API uint8_t     PropertyGetOr(const std::string& key, uint8_t  defVal);
CORE_API uint16_t    PropertyGetOr(const std::string& key, uint16_t defVal);
CORE_API uint32_t    PropertyGetOr(const std::string& key, uint32_t defVal);
CORE_API uint64_t    PropertyGetOr(const std::string& key, uint64_t defVal);
CORE_API float       PropertyGetOr(const std::string& key, float    defVal);
CORE_API double      PropertyGetOr(const std::string& key, double   defVal);


CORE_API bool PropertyForeach(void (*propFunc)(const std::string& key, 
                              const std::string& val, void* opaque),
                              void* opaque);

CORE_API bool PropertyGetList(PropertyList& proplist);

/**
 * wait until the property change to expect value or time out.
 * timeoutMs: timeout duration, -1: wait for ever, >= 0: wait duration
 *
 */
CORE_API bool PropertyWaitForProperty(const std::string& key,
                                      const std::string& expectVal,
                                      int32_t timeoutMs = -1);

/**
 * wait until the property creation or time out
 * timeoutMs: timeout duration, -1: wait for ever, >= 0: wait duration
 */
CORE_API bool PropertyWaitForPropertyCreation(const std::string& key, 
                                              int32_t timeoutMs = -1);

/**
 * wait until the property changed(don't care about change to what) or time out
 * timeoutMs: timeout duration, -1: wait for ever, >= 0: wait duration
 */
CORE_API bool PropertyWaitForPropertyChange(const std::string& key, 
                                            int32_t timeoutMs = -1);


} /** namespace sys */
} /** namespace panda*/

#endif /** !__PANDA_SYS_PROPERTY_H__ */
